package com.peza.bikelog.business.convertor;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.apache.log4j.Logger;

import com.peza.bikelog.common.domain.polar.HrmData;

public class PddConverter {

	private static final String INPUT_DIR = "/Users/Peter/Polar/Peter Derveaux/2009/";
	private static final String OUTPUT_DIR = "/Users/Peter/Polar/Peter Derveaux/2009/exports/";

	private static Logger logger = Logger.getLogger("Converter.class");

	public static void main(String[] args) throws Exception {

		
		// Add every .hrm file in the directory to a List

		List<File> inputFiles = new ArrayList<File>();
		File inDir = new File(INPUT_DIR);
		logger.debug("indir : " + inDir.getAbsolutePath());
		File[] fileArray = inDir.listFiles();
		for (File file : fileArray) {
			if (file.getName().endsWith(".hrm")) {
				inputFiles.add(file);
			}
		}
		logger.debug("found " + inputFiles.size() + " .hrm files");

		// Open every hrm file from the list and parse to a PolarData object
		for (File file : inputFiles) {
		//for (int i = 0; i < 1; i++){
			//File file = inputFiles.get(i);
			//file = new File(INPUT_DIR+"12010101.hrm");
			// find distance
			StringBuffer buffer = new StringBuffer();
			BufferedReader reader = new BufferedReader(new FileReader(file));
			
			String line = null;
			while ((line = reader.readLine()) != null) {
				buffer.append(line+"\n");
			}
			
			HrmData hrmData = new HrmData(buffer.toString());
			float distance = ((float)hrmData.getTrip().getDistance())/10; //kms
			float duration = ((float)hrmData.getTrip().getTotalTime())/3600; // hrs
		
			if (distance == 0){
				
				String fileName = file.getName().substring(0,file.getName().length()-4);
				String baseName = fileName;
				String sequence = "";
				Integer seq = null;
				if (fileName.length() > 6){
					sequence = fileName.substring(6,fileName.length());
					seq = Integer.parseInt(sequence);
					baseName = fileName.substring(0,6);
				}
				
				String pddName =  "20" + baseName + ".pdd";
				//System.out.println("basename : ["+baseName+"]");
				//System.out.println("pddname : ["+pddName+"]");
				File pddFile = new File(INPUT_DIR + pddName);
				BufferedReader pddReader = new BufferedReader(new FileReader(pddFile));
				String pddLine = null;
				while ((pddLine = pddReader.readLine()) != null) {
					if (pddLine.startsWith("[ExerciseInfo"+seq+"]")) {
						//System.out.println("Found : " + pddLine);
						pddReader.readLine(); //line1
						pddReader.readLine(); //line2
						pddReader.readLine(); //line3
						String pddData = pddReader.readLine(); //line4
						//System.out.println("pddData : ["+pddData+"]");
						String pddDistance = pddData.substring(0,pddData.indexOf("\t"));
						//System.out.println("PDD distance : ["+pddDistance+"]");
						distance = ((float)Integer.parseInt(pddDistance))/1000;
					}	
				}
				
				float avs = distance/duration;
				int avsInt = (int) (avs*10);
				float avst = ((float)avsInt)/10;
				
				// modify the file components:
				// modify sMode
				hrmData.enableSpeed();
				
				// modify hr data
				hrmData.getHrData().setAverageSpeed(avsInt);
				
				// modify trip data
				hrmData.getTrip().setDistance((int) (distance*10));
				hrmData.getTrip().setAvgSpeed(avsInt);
				
				
				File newDir = new File(OUTPUT_DIR);
				if (!newDir.exists()){
					newDir.mkdir();
				}
				File newFile = new File(OUTPUT_DIR+baseName+"0"+seq+".hrm");
				if (!newFile.exists()){
					newFile.createNewFile();
				}
				FileWriter writer = new FileWriter(newFile);
				writer.write(hrmData.toFileString());
				writer.close();

			}
			System.out.println(file.getName() + " : " + distance);
		}		
	}
}	
	